package com.reg.nstreg;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class net {
    interface CallBack {
        void onFailure(String arg1, String arg2);

        void onResponse(int arg1, String arg2, JSONObject arg3, String arg4);
    }

    CallBack _callBack;
    public static String cooDomain = "";
    List coooo_xjp;
    public static String v = "4";
    public static String web_userAgent;

    static {
    }

    public net() {
        super();
        this.coooo_xjp = new ArrayList();
    }

    public static List ConvertToCookies_xjp(String arg6) {
        Cookie v3_1 = null;
        JSONArray v1;
        JSONArray v0 = null;
        try {
            StringBuilder v2 = new StringBuilder();
            v2.append("");
            v2.append(arg6);
            v1 = new JSONArray(v2.toString());
        }
        catch(JSONException v6) {
            v6.printStackTrace();
            v1 = v0;
        }

        ArrayList v6_1 = new ArrayList();
        int v2_1;
        for(v2_1 = 0; v2_1 < v1.length(); ++v2_1) {
            try {
                v3_1 = Cookie.parse(HttpUrl.get(net.cooDomain), v1.getJSONObject(v2_1).getString("cookie"));
            }
            catch(JSONException v3) {
                v3.printStackTrace();
                Object v3_2 = v0;
            }

            ((List)v6_1).add(v3_1);
        }

        return ((List)v6_1);
    }

    public static JSONArray CookieToJson(List arg4) {
        JSONArray v0 = new JSONArray();
        Iterator v4 = arg4.iterator();
        while(v4.hasNext()) {
            String v1 = v4.next().toString();
            JSONObject v2 = new JSONObject();
            try {
                v2.put("cookie", v1);
            }
            catch(JSONException v1_1) {
                v1_1.printStackTrace();
            }

            v0.put(v2);
        }

        return v0;
    }
    CookieJar cookieJar;
    public void setNet(String arg7, String arg8, String arg9, final String arg10, String arg11, String arg12) {
        HashMap v0 = new HashMap();
        v0.put("_v_", net.v);
        v0.put("pkg", "com.reg.nstreg");
        v0.put("version_app", "23");
        v0.put("userAgent_", "" + net.web_userAgent);
        v0.put("time", "" + System.currentTimeMillis());
       StringBuilder v1 = new StringBuilder();
        v1.append("");
        StringBuilder v3 = new StringBuilder();
        v3.append("");
        v3.append(System.currentTimeMillis());
        v3.append(arg9);
        v1.append(net.sha256(v3.toString()));
        v0.put("ttpod", v1.toString());
        v0.put("fcm", "" + MainActivity.fcm);
        try {
            JSONArray v1_1 = new JSONArray(arg12);
            int v12_1;
            for(v12_1 = 0; v12_1 < v1_1.length(); ++v12_1) {
                v0.put(v1_1.getJSONObject(v12_1).getString("key"), v1_1.getJSONObject(v12_1).getString("value"));
            }
        }
        catch(JSONException v12) {
            v12.printStackTrace();
        }

        final HashMap hashMap = new HashMap();
        cookieJar = new CookieJar() {
            public List loadForRequest(HttpUrl arg3) {
                String v0 = arg10;
                if (v0 != null && !v0.equals("")) {
                    return net.ConvertToCookies_xjp(arg10);
                }

                Object v3 = hashMap.get(arg3);
                if (v3 != null) {
                } else {
                    ArrayList v3_1 = new ArrayList();
                }

                return ((List) v3);
            }

            public void saveFromResponse(HttpUrl arg2, List arg3) {
                if (arg3 != null && arg3.size() > 0) {
                    net.this.coooo_xjp = new ArrayList();
                    net.this.coooo_xjp.addAll(((Collection) arg3));
                }
            }
        };
        OkHttpClient v10 = new Builder().connectTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).cookieJar(cookieJar).build();
        Headers v12_2 = Headers.of(((Map)v0));
        Request v7 = arg8.equals("post") ? new okhttp3.Request.Builder().url(arg7).headers(v12_2).post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), arg11)).build() : new okhttp3.Request.Builder().url(arg7).headers(v12_2).build();
        v10.newCall(v7).enqueue(new Callback() {
            public void onFailure(Call arg2, IOException arg3) {
                if(net.this._callBack != null) {
                    net.this._callBack.onFailure(arg3.toString(), "action");
                }
            }

            public void onResponse(Call arg4, Response arg5) throws IOException {
                JSONException v5_1 = null;
                JSONObject v1_1;
                JSONObject v0_1;
                String v4 = GZipper.decodeBody(arg5);
                JSONObject v5 = null;
                try {
                    StringBuilder v1 = new StringBuilder();
                    v1.append("");
                    v1.append(v4);
                    v0_1 = new JSONObject(v1.toString());
                }
                catch(JSONException v0) {
                    v0.printStackTrace();
                    v0_1 = v5;
                }

                try {
                    v1_1 = new JSONObject("{}");
                }
                catch(JSONException v0) {
                    v1_1 = v5;
                    v5_1 = v0;
//                    goto label_33;
                }

                try {
                    String v5_2 = "response";
                    if(v0_1 != null) {
                    }
                    else {
                        String v0_2 = v4;
                    }

                    v1_1.put(v5_2, v0_1);
                    v1_1.put("cookie", net.CookieToJson(net.this.coooo_xjp));
//                    goto label_34;
                }
                catch(JSONException w) {
                }

//                label_33:
                v5_1.printStackTrace();
//                label_34:
                if(net.this._callBack != null) {
                    net.this._callBack.onResponse(200, "action", v1_1, v4);
                }
            }
        });
    }

    public void setOnCallBack(CallBack arg1) {
        this._callBack = arg1;
    }

    public static String sha256(String arg5) {
        try {
            byte[] v5_1 = MessageDigest.getInstance(GZipper.s).digest(arg5.getBytes(GZipper.u));
            StringBuffer v0 = new StringBuffer();
            int v1;
            for(v1 = 0; v1 < v5_1.length; ++v1) {
                String v2 = Integer.toHexString(v5_1[v1] & 0xFF);
                if(v2.length() == 1) {
                    v0.append('0');
                }

                v0.append(v2);
            }

            return v0.toString();
        }
        catch(Exception v5) {
            throw new RuntimeException(((Throwable)v5));
        }
    }
}

